package io.ukoko.layuibook.controller;


import io.ukoko.layuibook.dto.Result;
import io.ukoko.layuibook.entity.Book;
import io.ukoko.layuibook.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
public class BookController {

    @Autowired
    private BookService bookService;


    //添加图书
    @PostMapping(value = "/addBook")
    public Result addBook(@RequestBody Book book) {
        bookService.addBook(book);
        return Result.ok();
    }

    //删除图书(单个)
    @GetMapping(value = "/deleteBook")
    public Result deleteBook(Integer bookId){
        bookService.deleteBookByBookId(bookId);
        return Result.ok();
    }



    //删除图书(批量)
    @PostMapping(value = "/batchDeleteBook")
    public Result batchDeleteBook(@RequestBody List<Integer> bookIds) {
        bookService.batchDeleteBook(bookIds);
        return Result.ok();
    }



    //更新图书
    @PostMapping(value = "/updateBook")
    public Result updateBook(@RequestBody Book book) {
        bookService.updateBook(book);
        return Result.ok();
    }


    //分页搜索

    @GetMapping(value = "/getBooksPage")
    public Result getBookspage( @RequestParam(value = "pageNo") Integer pageNo,
                                @RequestParam(value = "pageSize")   Integer pageSize,
                                    String bookName,
                                    Integer categoryId,
                                    Integer status,
                                    @DateTimeFormat(pattern = "yyyy/MM/dd HH:mm:ss") Date start,
                                    @DateTimeFormat(pattern = "yyyy/MM/dd HH:mm:ss") Date end){

        return Result.ok(bookService.getBookspage(pageNo, pageSize, bookName, categoryId, status, start, end));
    }

}
